<?php

namespace App\Http\Controllers;

use App\Models\ChallengeGame;
use App\Models\ChallengeGameDetail;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class ChallengeGameDetailController extends Controller
{
    public function getInfoAndDetail()
    {
        $gameId = request()->input('game_id', 0);

        $column = ['challenge_games.id',
            'usera.id as usera_id',
            'usera.name as usera_name',
            'userb.id as userb_id',
            'userb.name as userb_name',
            'challenge_games.game_datetime',
            'challenge_games.score_a',
            'challenge_games.score_b',
        ];

        $gameInfo = ChallengeGame::join('wechat_users as usera', function ($join) {
            $join->on('challenge_games.play_a', '=', 'usera.id');
        })
            ->join('wechat_users as userb', function ($join) {
                $join->on('challenge_games.play_b', '=', 'userb.id');
            })
            ->where('challenge_games.id', $gameId)
            ->select($column)
            ->first();

        $info = ChallengeGameDetail::where('game_id', $gameId)->first();
        if ($info) {
            $detailScore = false;
            if (!empty($info->score)) {
                $detailScore = json_decode($info->score, true);
            }
            $detailDesc = false;
            if (!empty($info->desc)) {
                $detailDesc = json_decode($info->desc, true);
                $disk = Storage::disk('qiniu');
                // $detailDesc['file'] = $disk->url($detailDesc['file']);
                $detailDesc['file'] = $disk->getDriver()->downloadUrl($detailDesc['file'], 'https');
            }

            return response()->json(['recode' => 1, 'game_info' => $gameInfo, 'detail_score' => $detailScore, 'detail_desc' => $detailDesc]);
        }
        return response()->json(['recode' => 0, 'msg' => '没有详情']);
    }
    public function getInfo()
    {
        $gameId = request()->input('game_id', 0);

        $info = ChallengeGameDetail::where('game_id', $gameId)->first();
        if ($info) {
            $detailScore = false;
            if (!empty($info->score)) {
                $detailScore = json_decode($info->score, true);
            }
            $detailDesc = false;
            if (!empty($info->desc)) {
                $detailDesc = json_decode($info->desc, true);
                $detailDesc['file'] = $detailDesc['file'];
            }

            return response()->json(['recode' => 1, 'detail_score' => $detailScore, 'detail_desc' => $detailDesc]);
        }
        return response()->json(['recode' => 0, 'msg' => '没有详情']);
    }

    public function setScore()
    {
        $gameId = request()->input('game_id', 0);
        $score = request()->input('score', '');
        $info = ChallengeGameDetail::firstOrCreate(
            ['game_id' => $gameId], ['score' => $score]
        );
        if ($info->score == null) {
            $info->score = $score;
            $info->save();
        }

        return response()->json(['recode' => 1, 'msg' => '更新成功']);
    }
    public function setDesc(Request $request)
    {
        $file = $request->file("file");
        $gameId = request()->input('game_id');
        $type = request()->input('type');

        if ($file) {
            $disk = Storage::disk('qiniu');
            $filename = $disk->put('game/', $file);
            // $url = $disk->url($filename);
            $desc = ['type' => $type, 'file' => $filename];
            $descStr = json_encode($desc);
            $info = ChallengeGameDetail::firstOrCreate(
                ['game_id' => $gameId], ['desc' => $descStr]
            );
            if ($info->desc == null) {
                $info->desc = $descStr;
                $info->save();
            }
            return response()->json(['recode' => 1, 'msg' => '更新成功']);
        } else {
            return response()->json(['recode' => 0, 'msg' => '文件不能为空']);
        }
    }
}
